package com.sbj.dto.utils;

import cn.hutool.core.date.LocalDateTimeUtil;
import cn.hutool.cron.CronException;

import java.time.*;
import java.time.format.DateTimeFormatter;
import java.time.temporal.ChronoUnit;

/**
 * @author summer
 * @version 1.0
 * @date 2024/8/28 22:15
 */
public class LocalDateTimeUtils {

    public static void main(String[] args) {

        LocalDateTime now = LocalDateTime.now();
        System.out.println(now.format(DateTimeFormatter.ofPattern("HH:mm")));


    }

    public static boolean isTimeInRange(LocalTime start, LocalTime end, LocalTime time) {
        return time.isAfter(start) && time.isBefore(end) ;
    }

    public static boolean isDateTimeInRange(LocalDateTime start, LocalDateTime end, LocalDateTime time) {
        return time.isAfter(start) && time.isBefore(end);
    }

    public static Long between(LocalTime start, LocalTime end) {
        return LocalDateTimeUtil.between(LocalDateTime.of(LocalDate.now(),start), LocalDateTime.of(LocalDate.now(), end), ChronoUnit.SECONDS);
    }


    public static String convertToCronExpression(LocalTime localTime) {
        DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("ss mm HH");
        String format = localTime.format(dateTimeFormatter);
        CronException cronException = new CronException(format);
        return cronException.getMessage();

    }


    public static Long countDown(LocalTime localTime,Integer countDown){
        long count=0L;
        LocalDateTime firstDateTime = LocalDateTime.of(LocalDate.now(), localTime);
        if(localTime.isBefore(LocalTime.now())){
            count= LocalDateTimeUtil.between(LocalDateTime.now(), LocalDateTime.of(LocalDate.now().plusDays(1), localTime), ChronoUnit.SECONDS);
        }else{
            count= LocalDateTimeUtil.between(LocalDateTime.now(), firstDateTime, ChronoUnit.SECONDS);
        }
        return count<=countDown * 60 ?count:0;
     }
}